<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/swiper.css">
    <script src="js/vue.js"></script>
    <script src="js/swiper.js"></script>
    <style>
        .box {
            width: 800px;
            height: 300px;
            background-color: yellow;
            margin: auto;
        }
    </style>
</head>

<body>
    <div id="box">
        <!-- 基本结构 -->
        <div class="swiper-container box">
            <div class="swiper-wrapper">
                <!-- v-swiper自定义指令 -->
                <div class="swiper-slide" v-for="(item,index) in dataList" v-swiper="{
                    current:index,
                    length:dataList.length
                }">{{item}}</div>
            </div>
            <div class="swiper-button-next"></div>
            <div class="swiper-button-prev"></div>
        </div>
    </div>

    <script>
        Vue.directive("swiper", {
            inserted(el, binding) {
                if (binding.value.current === binding.value.length - 1) {
                    new Swiper(".box", {
                        loop: true,
                        navigation: {
                            nextEl: '.swiper-button-next',
                            prevEl: '.swiper-button-prev',
                        }
                    })
                }
            }
        })
        var vm = new Vue({
            el: "#box",
            data: {
                dataList: []
            },
            created() {
                var _this = this;
                setTimeout(function() {
                    _this.dataList = ["首页", "商品", "详情", "购物车", "用户中心"];
                }, 2000)
            }
        })
    </script>
</body>

</html>